package fr.castorhoz.isi_connectivity_widget.wifi_widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
import fr.castorhoz.isi_connectivity_widget.ConnBroadcastReceiver;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.database.DBAdapter;

public class WifiBroadcastReceiver extends ConnBroadcastReceiver {

	private static List<Integer> appWidgetIds = new ArrayList<Integer>();
	
	private DBAdapter dbAdapter;

	public WifiBroadcastReceiver(Context context) { //ConnButton button) {
		super(context); //button);
		dbAdapter = new DBAdapter(context);
		dbAdapter.open();
		appWidgetIds = dbAdapter.getAppWidgetIdUsingWidget(ConnButton.widgets.wifiWidget.name());
		dbAdapter.close();
	}
	
	/**
	 * Adds the appWidget to the ones that need to be notified when the broadcast receiver receives an event
	 * @param appWidgetId
	 */
	public static void addAppWidget(int appWidgetId) {
		Log.d(WifiBroadcastReceiver.class.getName(), "adding appWidgetId "+appWidgetId+" to WifiBroadcastReceiver");
		appWidgetIds.add(appWidgetId);
	}
	
	@Override
	public void doOnReceive(Context context, Intent intent) {
		Log.i(this.getClass().getName(), "looking at wifi state");
		/*final String action = intent.getAction();

		if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
			Log.i(this.getClass().toString(), "wifi changed");

			int info = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);

			// METTRE A JOUR LES IMAGES ET LES ETATS DES COMPONENTS
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);
			int selector = button.getSelectorUnknown();

			if (info == (WifiManager.WIFI_STATE_ENABLED)) {
				Log.i(this.getClass().toString(), "Status of wifi : connected");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : enabled", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorOn();
				button.setState(ConnButton.STATE_ON);
			} else if (info == (WifiManager.WIFI_STATE_ENABLING)) {
				Log.i(this.getClass().toString(), "Status of wifi : connecting");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : enabling", Toast.LENGTH_SHORT).show();
				selector = WifiButton.selector_on;
				button.setState(ConnButton.STATE_ON);
			} else if (info == (WifiManager.WIFI_STATE_DISABLED)) {
				Log.i(this.getClass().toString(), "Status of wifi : disabled");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : disconnected", Toast.LENGTH_SHORT)
						.show();
				selector = button.getSelectorOff();
				button.setState(ConnButton.STATE_OFF);
			} else if (info == (WifiManager.WIFI_STATE_DISABLING)) {
				Log.i(this.getClass().toString(), "Status of wifi : disabling");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : disconnecting", Toast.LENGTH_SHORT)
						.show();
				selector = button.getSelectorOff();
			} else if (info == (WifiManager.WIFI_STATE_UNKNOWN)) {
				Log.i(this.getClass().toString(), "Status of wifi : unknown");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : unknown", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorUnknown();
			}
			//remoteViews.setImageViewResource(button.getIdButton(), selector);
			remoteViews.setImageViewResource(R.id.imageButtonWidgetRow, selector);

		}*/
		
		int info = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
		boolean previousState = WifiButton.getWifiStateFromDatabse(context);
		switch (info) {
		case WifiManager.WIFI_STATE_ENABLED:
			if (!previousState) {
				Log.i(this.getClass().getName(), "registering wifi state : WIFI_STATE_ENABLED");
				previousState = !previousState;
				changeWifiDBValue(WifiButton.WIFI_DISABLED);
				hasChanged();
			}
			break;
			
		case WifiManager.WIFI_STATE_DISABLED:
			if (previousState) {
				Log.i(this.getClass().getName(), "registering wifi state : WIFI_STATE_DISABLED");
				changeWifiDBValue(WifiButton.WIFI_ENABLED);
				previousState = !previousState;
				hasChanged();
			}
			break;
			
		default:
			break;
		}
	}
	
	private void changeWifiDBValue(String value) {
		dbAdapter.open();
		dbAdapter.removeWifiWidgetStates();
		dbAdapter.insertWifiWidgetState(value);
		dbAdapter.close();
		Log.d(this.getClass().getName(), "wifi state is saved into database");
	}

	@Override
	protected List<Integer> getAppWidgetIds() {
		return appWidgetIds;
	}

//	@Override
//	protected void registerItself(Context context) {
//		/*IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
//		context.getApplicationContext().registerReceiver(this, filter);*/
//		//Log.d(this.getClass().getName(), "broadcast receiver "+this.getClass().getCanonicalName()+" has registered itself");
//	}

	@Override
	public String getBroadcastAction() {
		return WifiManager.WIFI_STATE_CHANGED_ACTION;
	}

	@Override
	protected String getWidgetName() {
		return ConnButton.widgets.wifiWidget.name();
	}

}
